3ee257
@@ -19,6 +19,7 @@
 package org.apache.hadoop.hive.ql.udf.generic;
 
 import org.apache.hadoop.hive.ql.exec.Description;
+import org.apache.hadoop.hive.ql.exec.FunctionRegistry;
 import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
 import org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException;
 import org.apache.hadoop.hive.ql.udf.generic.GenericUDFUtils.ReturnObjectInspectorResolver;
@@ -132,8 +133,22 @@
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumen
 
       if (oiTypeInfo0 != oiTypeInfo1) {
         compareType = CompareType.NEED_CONVERT;
-        compareOI = TypeInfoUtils.getStandardWritableObjectInspectorFromTypeInfo(
-            TypeInfoFactory.doubleTypeInfo);
+
+        // If either argument is a string, we convert to a double because a number
+        // in string form should always be convertible into a double
+        if (oiTypeInfo0.equals(TypeInfoFactory.stringTypeInfo)
+            || oiTypeInfo1.equals(TypeInfoFactory.stringTypeInfo)) {
+          compareOI = TypeInfoUtils.getStandardWritableObjectInspectorFromTypeInfo(
+              TypeInfoFactory.doubleTypeInfo);
+        } else {
+          TypeInfo compareType = FunctionRegistry.getCommonClass(oiTypeInfo0, oiTypeInfo1);
+
+          // For now, we always convert to double if we can't find a common type
+          compareOI = TypeInfoUtils.getStandardWritableObjectInspectorFromTypeInfo(
+              (compareType == null) ?
+              TypeInfoFactory.doubleTypeInfo : compareType);
+        }
+
         converter0 = ObjectInspectorConverters.getConverter(arguments[0], compareOI);
         converter1 = ObjectInspectorConverters.getConverter(arguments[1], compareOI);
       } else {
